﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MultiSliderSample
{
    /// <summary>
    /// MultiSlider.xaml 的交互逻辑
    /// </summary>
    public partial class MultiSlider : UserControl
    {
        #region 字段
        private double _maxWidth = 330;
        private double _height = 40;
        private List<Rectangle> _rectangles;
        private List<ThumbEx> _thumbExs;
        #endregion

        #region 构造
        public MultiSlider()
        {
            InitializeComponent();
            _rectangles = new List<Rectangle>();
            _thumbExs = new List<ThumbEx>();
        }
        #endregion

        #region 依赖属性
        public ObservableCollection<RangeItem> RangeItems
        {
            get
            {
                return (ObservableCollection<RangeItem>)GetValue(RangeItemsProperty);
            }
            set
            {
                SetValue(RangeItemsProperty, value);
            }
        }

        // Using a DependencyProperty as the backing store for RangeItems.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RangeItemsProperty =
            DependencyProperty.Register("RangeItems", typeof(ObservableCollection<RangeItem>), typeof(MultiSlider), new PropertyMetadata(null,
                (s, e) =>
                {
                    var timeRange = s as MultiSlider;

                    if (timeRange != null)
                    {
                        timeRange.BuildRange();
                    }
                }));
        #endregion

        #region 私有方法

        #region 构造时间段
        private void BuildRange()
        {
            if (this.RangeItems.Count == 1)
            {
                BuildSingleRange();
            }
            else
            {
                BuildMultiRange();
            }
        }
        #endregion

        #region 构造一整块时间段

        private void BuildSingleRange()
        {
            this.RangeContainer.Children.Clear();

            var item = RangeItems.FirstOrDefault();

            if (item != null)
            {
                var rectWidth = _maxWidth;

                var rect = new Rectangle()
                {
                    Width = rectWidth,
                    Margin = new Thickness(0, 40, 0, 40),
                    Stroke = Brushes.Transparent,
                    StrokeThickness = 0
                };

                rect.Fill = GetColor(item);

                this.RangeContainer.Children.Add(rect);
            }

        }

        #endregion

        #region 构造多个时间段

        private void BuildMultiRange()
        {
            this.RangeContainer.Children.Clear();

            //构造时间段的矩形
            foreach (var item in this.RangeItems)
            {
                var rectWidth = (item.To - item.From) / item.MaxDuration * _maxWidth;

                var rect = new Rectangle()
                {
                    Width = rectWidth,
                    Margin = new Thickness(0, 40, 0, 40),
                    Stroke = Brushes.Transparent,
                    StrokeThickness = 0
                };

                rect.Fill = GetColor(item);

                this.RangeContainer.Children.Add(rect);
                _rectangles.Add(rect);
            }

            //滑块起始位置
            var leftPosition = 0d;

            //构造时间控制的滑块
            for (int i = 0; i < _rectangles.Count - 1; i++)
            {
                var leftRect = _rectangles[i];
                var rightRect = _rectangles[i + 1];

                leftPosition += leftRect.Width;

                var thumb = new ThumbEx(leftRect, rightRect, RangeItems[i], RangeItems[i + 1]);

                Canvas.SetLeft(thumb, leftPosition);

                this.ThumbContainer.Children.Add(thumb);
                _thumbExs.Add(thumb);

                thumb.ThumbExs = _thumbExs;
            }
        }

        #endregion

        #region 获取时间段颜色

        private SolidColorBrush GetColor(RangeItem item)
        {
            if (item.IsStatic)
            {
                return Brushes.Green;
            }
            else
            {
                return Brushes.DarkRed;
            }
        }

        #endregion

        #endregion


    }
}
